package com.valuablecode.test;

import java.util.*;
import static java.util.Calendar.*;

/**
 * Knows how to create a date time by combining a date with a time specification.
 * 
 * <p> Typical usage in test code is:
 * <blockquote><pre>
 * assertThat(billingPolicy.overdue(), equalTo(on(april(30, 2010)).at("06:30")));</pre></blockquote>
 * 
 * @author Alistair McKinnell
 */
public class DateTimeFactory {

    private final Date date;

    private DateTimeFactory(Date date) {
        this.date = date;
    }

    public static DateTimeFactory on(Date date) {
        return new DateTimeFactory(date);
    }

    public Date at(String timeSpecificationSource) {
        Calendar current = currentCalendar();
        TimeSpecification timeSpecification = TimeSpecification.parse(timeSpecificationSource);
        
        GregorianCalendar result = new GregorianCalendar(current.get(YEAR), current.get(MONTH), current.get(DAY_OF_MONTH),
                        timeSpecification.getHours(), timeSpecification.getMinutes(), timeSpecification.getSeconds());
        
        return result.getTime();
    }

    private Calendar currentCalendar() {
        Calendar result = Calendar.getInstance();
        result.setTime(date);
        
        return result;
    }

}
